﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Willson.JsbManager.Bll;
using Willson.JsbManager.Comm;
using Willson.JsbManager.BllEntity;

namespace Willson.JsbManager.UI
{
    public partial class XTQueryForm : Willson.JsbManager.UI.BaseForm
    {
        private BindingList<BEXTQueryMainInfo> MainList = null;
               
        public XTQueryForm()
        {
            InitializeComponent();

            comboBox_type.SelectedIndex = 0;
        }

        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox_text.Text = string.Empty;
            this.textBox_text.Focus();
        }

        private void button_search_Click(object sender, EventArgs e)
        {
            if (this.textBox_text.Text.Length == 0)
            {
                this.textBox_text.Focus();
                return;
            }

            

            if (this.comboBox_type.Text == "拼音码")
            {

                MainList = BLLFactory<BLLXTQuery>.Instance.GetXTQueryMainInfo(this.textBox_text.Text, QueryCompanyType.Pym);

            }
            else
            {
                MainList = BLLFactory<BLLXTQuery>.Instance.GetXTQueryMainInfo(this.textBox_text.Text, QueryCompanyType.Name);  
            }

            this.dataGridView_main.DataSource = MainList;
            label_search_result.Text = "共查询到 " + dataGridView_main.RowCount.ToString() + " 条记录";
            dataGridView_main.Focus();
        }

        private void dataGridView_main_Resize(object sender, EventArgs e)
        {
            dataGridView_main.Columns["company_name"].Width = dataGridView_main.Width - dataGridView_main.Columns["company_gm_name"].Width -
                dataGridView_main.Columns["company_telphone"].Width - dataGridView_main.Columns["company_address"].Width - dataGridView_main.RowHeadersWidth;
        }

        private void textBox_text_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) button_search_Click(sender, e);
        }

        private void dataGridView_main_SelectionChanged(object sender, EventArgs e)
        {
            int ll_company_id = 0;

            if (MainList != null)
            {
                if (MainList.Count == 0)
                    ll_company_id = 0;
                else
                    ll_company_id = int.Parse(dataGridView_main.Rows[dataGridView_main.CurrentCell.RowIndex].Cells["id"].Value.ToString());

            }
            
            switch (this.tabControl.SelectedIndex)
            {
                case 0: company_info_view(ll_company_id); break;
                case 1: Delegate_bank_info_view(ll_company_id); break;
                case 2: Company_sb_info_view(ll_company_id); break;
                case 3: Company_sb_jd_info_view(ll_company_id); break;
            }
        }

        private void Company_sb_jd_info_view(int company_id)
        {
            BindingList<BEXTQuerySbJdInfo> SbJdList = null;

            if(company_id > 0)
                SbJdList = BLLFactory<BLLXTQuery>.Instance.GetXTQuerySbJdInfo(company_id);

            this.dataGridView_sb_jd.DataSource = SbJdList;

        }

        private void Company_sb_info_view(int company_id)
        {
            BindingList<BEXTQuerySbInfo> SbList = null;

            if ( company_id > 0 )
                SbList = BLLFactory<BLLXTQuery>.Instance.GetXTQuerySbInfo(company_id);

            this.dataGridView_sb_info.DataSource = SbList;
        }

        private void Delegate_bank_info_view(int company_id)
        {
            BEXTQueryDelegateBankInfo info = null;

            if (company_id > 0)
                info = BLLFactory<BLLXTQuery>.Instance.GetXTQueryDelegateBankInfo(company_id);

            try
            {
                delegateBankBindingSource.DataSource = info;
            }
            catch
            {
                ;
            }

            
        }

        private void company_info_view(int company_id)
        {
            BEXTQueryCompanyInfo info = null;

            if (company_id > 0)
                info = BLLFactory<BLLXTQuery>.Instance.GetXTQueryCompanyInfo(company_id);

            try
            {
                CompanyInfoBindingSource.DataSource = info;
            }
            catch
            {
                ;
            }
 
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView_main_SelectionChanged(sender, e);
        }

        private void dataGridView_sb_info_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int ll_sb_id = int.Parse(dataGridView_sb_info.Rows[dataGridView_sb_info.CurrentCell.RowIndex].Cells["sb_id"].Value.ToString());

            CompanySbMonthInfoForm form = new CompanySbMonthInfoForm(ll_sb_id);

            form.ShowDialog();
        }

        private void XTQueryForm_Activated(object sender, EventArgs e)
        {
            this.textBox_text.Focus();
        }
   
    }
}
